class GlobeRotate {
  static _normal_duration = 24 * 60 * 60;
  constructor(viewer, options) {
    this._viewer = viewer;
    this._duration = options?.duration || 90
    if (!GlobeRotate.instance) {
      GlobeRotate.instance = this;
    }
    return GlobeRotate.instance;
  }
  // 开始
  start() {
    this._viewer.clock.shouldAnimate = true;
    this._unbindEvent();
    this._bindEvent();
    return this;
  }
  //  解除绑定
  _unbindEvent() {
    this._viewer.clock.multiplier = 1;
    this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
    this._viewer.scene.postUpdate.removeEventListener(this._icrf, this);
  }
  //  绑定事件
  _bindEvent() {
    this._viewer.clock.multiplier = GlobeRotate._normal_duration / this._duration;
    this._viewer.camera.lookAtTransform(Cesium.Matrix4.IDENTITY);
    this._viewer.scene.postUpdate.addEventListener(this._icrf, this);
  }
  _icrf() {
    // 
    if (this._viewer.scene.mode !== Cesium.SceneMode.SCENE3D) {
      return true
    }
    const icrfToFixed = Cesium.Transforms.computeIcrfToFixedMatrix(this._viewer.clock.currentTime);
    if (!icrfToFixed) { return }
    const camera = this._viewer.camera;
    const offset = Cesium.Cartesian3.clone(camera.position);
    const transform = Cesium.Matrix4.fromRotationTranslation(icrfToFixed);
    camera.lookAtTransform(transform, offset);
  }
}

function createGlobeRotate(viewer, options) {
  if (!viewer) { return }
  return new GlobeRotate(viewer, options);
}

export default { createGlobeRotate }
